﻿var salepwd = {
    init: function() {
        var that = this;
        that.initValidateRule();
        that.doSubmit();
    },

    doSubmit: function() {
        var that = this;
        $("#modify-pwd").validate({
            rules: {
                oldpwd: 'required',
                newpwd: 'required',
                rp_newpwd: 'repeatpwd',
            },
            messages: {
                oldpwd: '请输入原密码',
                newpwd: '请输入新密码',
                rp_newpwd: '请确认密码'
            },
            submitHandler: function(form) {
                // alert('submitHandler');
                $.ajax({
                    url: COMMONURL + "api/Account/EditEmailAndPhone",
                    type: "post",
                    dataType: "json",
                    data: {
                        account: $.cookie('account'),
                        password: $('[name=oldpwd]').val(),
                        newpassword: $('[name=newpwd]').val(),
                    }
                }).done(function(data) {
                    if (data.recode == 0000) {
                        alert("修改密码成功");
                        that.clear();
                    } else {
                        alert(data.resmessage);
                    }
                })
            }
        })
    },

    initValidateRule: function() {
        // 添加重复密码的规则
        jQuery.validator.addMethod("repeatpwd", function(value, element) {
            if(value === $('#newpwd').val())
                return true;
            else
                return false;
        }, '密码输入与第一次不一致');
    },
    clear: function() {
        $('[name=oldpwd]').val('');
        $('[name=newpwd]').val('');
        $('[name=rp_newpwd]').val('');
    }

}

salepwd.init();
